package com.link.base.base.approval.controller;

import com.link.base.base.approval.model.ApprovalRule;
import com.link.base.base.approval.service.ApprovalRuleService;
import com.link.core.basic.annotations.JsonParam;
import com.link.core.basic.controller.BasicController;
import com.link.core.basic.model.BasicModel;
import com.link.core.basic.query.QueryParams;
import com.link.core.basic.service.BasicService;
import com.link.core.basic.service.KeyGenerateService;
import com.link.core.cllog.LCLogger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 审批规则-审批规则 
 *
 * @author 姚江漪
 * @date 2019/05/29 14:12
 */
@Controller
@RequestMapping("/link/approvalRule")
public class ApprovalRuleController extends BasicController<ApprovalRule> {
 
    @Resource
    private KeyGenerateService keyGenerateService;

    @Resource
    private ApprovalRuleService approvalRuleService;

    @Override
    public BasicService<ApprovalRule> getBasicService() throws Exception {
        return approvalRuleService;
    }

    /**
     * 查询未关联该流程的规则
     * @author 姚江漪
     * @date 2019/05/29 19:45
     */
    @RequestMapping(value = "/queryUnSelectFlowListPage")
    @ResponseBody
    public Map<String, Object> queryUnSelectFlowListPage(@JsonParam ApprovalRule approvalRule, @JsonParam QueryParams qps,
                                                         HttpSession session, HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = new HashMap<String, Object>(4);
        try {
            qps.preDealSecurity(request);
            ApprovalRule t = BasicModel.transformClass(approvalRule, qps);
            beforQueryAllOrExample(t, request);
            beforQueryExample(t, request);
            List<ApprovalRule> list = approvalRuleService.queryUnSelectFlowListPage(t);
            result.put("success", true);
            result.put("rows", list);
            result.put("total", getCount((BasicModel) t, list.size()));
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage() + "(BASE_AR_001)");
        }
        return result;
    }

    /**
     * 校验是否存在符合规则的审批流程
     *
     * @author 姚江漪
     * @date 2019/06/03 16:43
     */
    @RequestMapping(value = "/checkRules")
    @ResponseBody
    public Map<String, Object> checkRules(@JsonParam ApprovalRule approvalRule, @JsonParam QueryParams qps,
                                          HttpSession session, HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = new HashMap<String, Object>(4);
        try {
            approvalRuleService.checkRules(approvalRule.getFlowType(),approvalRule.getAttr1());
            result.put("success", true);
            result.put("result", "");
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage() + "(BASE_AR_002)");
        }
        return result;
    }
}
